package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cx
 * @date 2023/4/3 19:15
 * @Version 1.0
 */
@Service
public class WxCatalogServiceImpl implements WxCatalogService{

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public Map<String, Object> index() {
        List<MarketCategory> categoryList = queryCategoryList();
        MarketCategory currentCategory = queryCurrentCategory();
        List<MarketCategory> currentSubCategory = queryCurrentSubCategory();
        Map<String,Object> result = new HashMap<>();
        result.put("categoryList", categoryList);
        result.put("currentCategory", currentCategory);
        result.put("currentSubCategory", currentSubCategory);
        return result;
    }

    private List<MarketCategory> queryCurrentSubCategory() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.or().andPidEqualTo(1005000);
        return marketCategoryMapper.selectByExample(marketCategoryExample);
    }

    private MarketCategory queryCurrentCategory() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.or().andIdEqualTo(1005000);
        return marketCategoryMapper.selectOneByExample(marketCategoryExample);
    }

    @Override
    public Map<String, Object> current(Integer id) {
        MarketCategory currentCategory = queryCurrentCategory(id);
        List<MarketCategory> currentSubCategory = queryCurrentSubCategory(currentCategory.getId());
        Map<String, Object> data = new HashMap<>();
        data.put("currentCategory", currentCategory);
        data.put("currentSubCategory", currentSubCategory);
        return data;
    }

    private List<MarketCategory> queryCurrentSubCategory(Integer pid) {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.or().andPidEqualTo(pid);
        return marketCategoryMapper.selectByExample(marketCategoryExample);
    }

    private MarketCategory queryCurrentCategory(Integer id) {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.or().andIdEqualTo(id);
        return marketCategoryMapper.selectOneByExample(marketCategoryExample);
    }

    private List<MarketCategory> queryCategoryList() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.or().andPidEqualTo(0).andDeletedEqualTo(false);
        return marketCategoryMapper.selectByExample(marketCategoryExample);
    }


}
